package com.leeon.generics;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class Apply {

	public static <T,S extends Iterable<? extends T>> void 
		applay(S seq,Method m,Object...args){
		for(T t:seq){
			try {
				m.invoke(t, args);
			} catch (Exception e) {
				throw new RuntimeException();
			}
		}
	}
	
}


class Shape{
	public void rotate(){
		System.out.println(this + " rotate");
	}
	public void resize(int size){
		System.out.println(this + " resize " + size);
	}
}

class Square extends Shape{}

@SuppressWarnings("serial")
class FilledList<T> extends ArrayList<T>{
	public FilledList(Class<? extends T> type,int size){
		for (int i = 0; i < size; i++) {
			try {
				add(type.newInstance());
			} catch (Exception e) {
				throw new RuntimeException();
			}
		}
	}
}

class ApplyTest{
	public static void main(String[] args) throws Exception{
		List<Shape> shapes = new ArrayList<Shape>();
		for (int i = 0; i < 10; i++) {
			shapes.add(new Shape());
		}
		Apply.applay(shapes, Shape.class.getMethod("rotate"));
		Apply.applay(shapes, Shape.class.getMethod("resize", int.class), 5);
		
		Apply.applay(new FilledList<Shape>(Shape.class, 10), Shape.class.getMethod("rotate"));
		Apply.applay(new FilledList<Shape>(Shape.class, 10), Shape.class.getMethod("resize",int.class),20);
	}
}